package adapter;

import java.util.ArrayList;

import listviewdisplay.ImageLoader;

import com.ericdm.mydoubanmovie.R;
import com.ericdm.mydoubanmovie.TheWebView;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AnalogClock;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class BoxoffcieAdapter extends BaseAdapter{
	private ArrayList<String> imageViewList;
	private ArrayList<String> enNameViewList;
	private ArrayList<String> cnNameViewList;
	private ArrayList<String> rateViewList;
	private ArrayList<String> boxViewList;
	private ArrayList<String>rankViewList;
	private ArrayList<String>moviewUrl;
	private ArrayList<String>newMoviewViewList;
	private Context context;
	private LayoutInflater layoutInflater = null;
	private ImageButton imageButton = null;
	
	public BoxoffcieAdapter(Context context, ArrayList<String> iamgeViewList,ArrayList<String> cnNameViewList, ArrayList<String> enNameViewList, ArrayList<String> rateViewList, ArrayList<String> boxViewList,ArrayList<String>rankViewList, ArrayList<String>newMoviewViewList
			, ArrayList<String>moviewUrl) {
		this.context = context;
		this.imageViewList = iamgeViewList;
		this.cnNameViewList = cnNameViewList;
		this.enNameViewList = enNameViewList;
		this.rateViewList = rateViewList;
		this.boxViewList = boxViewList;
		this.rankViewList = rankViewList;
		this.moviewUrl = moviewUrl;
		this.newMoviewViewList = newMoviewViewList;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return imageViewList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return imageViewList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressWarnings("unused")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
//		convertView = layoutInflater.inflate(R.layout.boxoffice_listview, null);
		
		ViewHoler viewHoler = null;
		if (viewHoler == null) {
			viewHoler = new ViewHoler();
			convertView = layoutInflater.inflate(R.layout.boxoffice_listview, null);
			viewHoler.imageView = (ImageView)convertView.findViewById(R.id.poster);
			viewHoler.cnNameTextView = (TextView)convertView.findViewById(R.id.chnese_name_textview);
			viewHoler.enNameTextView = (TextView)convertView.findViewById(R.id.en_name_textview);
			viewHoler.rateTextView = (TextView)convertView.findViewById(R.id.rate_textview);
			viewHoler.boxTextView = (TextView)convertView.findViewById(R.id.boxoffcie_textview);
			viewHoler.rankTextView = (TextView)convertView.findViewById(R.id.rank_textview);
			viewHoler.newMoviewTextView = (TextView)convertView.findViewById(R.id.newMoview_textview);
			viewHoler.imageButton = (ImageButton)convertView.findViewById(R.id.my_image_button_);
			convertView.setTag(viewHoler);
		}else {
			viewHoler = (ViewHoler)convertView.getTag();
		}
		
		viewHoler.imageButton.setOnClickListener(new ImageButtonListener(position));
		ImageLoader imageLoader = new ImageLoader(context);
		imageLoader.displayImage(imageViewList.get(position), viewHoler.imageView);
		viewHoler.cnNameTextView.setText(cnNameViewList.get(position));
		viewHoler.enNameTextView.setText(enNameViewList.get(position));
		viewHoler.rateTextView.setText(rateViewList.get(position));
		viewHoler.boxTextView.setText(boxViewList.get(position));
		viewHoler.rankTextView.setText(rankViewList.get(position));
		viewHoler.newMoviewTextView.setText(newMoviewViewList.get(position));
		if (position %2 == 0) {
			viewHoler.cnNameTextView.setTextColor (android.graphics.Color.GREEN);
			viewHoler.enNameTextView.setTextColor (android.graphics.Color.GREEN);
			viewHoler.rateTextView.setTextColor (android.graphics.Color.GREEN);
			viewHoler.boxTextView.setTextColor (android.graphics.Color.GREEN);
			viewHoler.rankTextView.setTextColor (android.graphics.Color.GREEN);
		}else {
			viewHoler.cnNameTextView.setTextColor (android.graphics.Color.RED);
			viewHoler.enNameTextView.setTextColor (android.graphics.Color.RED);
			viewHoler.rateTextView.setTextColor (android.graphics.Color.RED);
			viewHoler.boxTextView.setTextColor (android.graphics.Color.RED);
			viewHoler.rankTextView.setTextColor (android.graphics.Color.RED);
		}
		viewHoler.newMoviewTextView.setTextColor (android.graphics.Color.YELLOW);
		return convertView;
	}
	
		class ImageButtonListener implements OnClickListener {
			private int clickedRow;
			ImageButtonListener(int pos){
				clickedRow = pos;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(context, TheWebView.class);
			intent.putExtra("moviewUrl", moviewUrl.get(clickedRow));
			context.startActivity(intent);
		}
	}
	
	public static class ViewHoler{
		public ImageView imageView;
		public TextView enNameTextView;
		public TextView cnNameTextView;
		public TextView rateTextView;
		public TextView boxTextView;
		public TextView rankTextView;
		public TextView newMoviewTextView;
		public ImageButton imageButton;
	}
}
